﻿using UnityEngine;

using System;

namespace KFGame
{
    public class KFTask
    {
        [Flags]
        public enum TasKType
        {
            /// <summary>
            /// 启动时运行
            /// </summary>
            onStart = 0x01,
            /// <summary>
            /// 运行过程中，需要计算时间间隔
            /// </summary>
            onRun = 0x02,
            /// <summary>
            /// 结束时运行
            /// </summary>
            onDestroy = 0x04
        }


        /// <summary>
        /// 任务名字
        /// </summary>
        public string taskName;

        /// <summary>
        /// 只运行一次
        /// </summary>
        public bool once = false;

        /// <summary>
        /// 完成任务
        /// </summary>
        public bool finish = false;

        public TasKType type;

        /// <summary>
        /// 运行计时
        /// </summary>
        public double runtime;

        /// <summary>
        /// 时间间隔，单位秒
        /// </summary>
        public double interval = 60;


        public void runstart()
        {
            if (finish)
                return;

            if ((type & TasKType.onStart) == TasKType.onStart)
            {
                task();
                runtime = Time.time;
                if (once)
                    finish = true;
            }
        }

        public void runDestroy()
        {
            if (finish)
                return;

            if ((type & TasKType.onDestroy) == TasKType.onDestroy)
            {
                task();
                runtime = Time.time;
                if (once)
                    finish = true;
            }
        }

        public void runing()
        {
            if (finish)
                return;



            if ((type & TasKType.onRun) == TasKType.onRun)
                if ((Time.time - runtime) >= interval)
                {
                    task();
                    runtime = Time.time;
                    if (once)
                        finish = true;

                }
        }

        public virtual void task()
        {

        }

    }
}